package com.ny.service.impl;

import com.ny.entity.User;
import com.ny.mapper.UserMapper;
import com.ny.service.EmailService;
import com.ny.service.UserService;
import com.ny.until.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
// 如果不需要 List 相关操作，可以删除这行导入
// import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int addUser(User user) {

        String pwd = PasswordUtil.encryptPassword(user.getPassword());
        PasswordUtil.encryptPassword(pwd);
        user.setPassword(pwd);
        return userMapper.addUser(user);

    }
  @GetMapping("/login")
    @Override
    public User findUser(String username, String password) {
//        String pwd = PasswordUtil.encryptPassword(password);
        return userMapper.findIser(username,password);
    }

    @Override
    public User findUserByEmail(String email) {
        return userMapper.findUserByEmail(email);
    }

    @Override
    public int updatePassword(String password, String email) {
        // 密码加密
        String pwd = PasswordUtil.encryptPassword(password);
        return userMapper.updatePassword(pwd,email);
    }

    @Override
    public void sendToEmail(String toEmail) {

    }
@Autowired
    private EmailService emailService;
    @Override
    public void sendEmail(String toEmail) {
        emailService.sendEmail(toEmail,emailService.getCode());
    }


@Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public  boolean checkCode(String toEmail, String code) {
        ValueOperations<String,String> ops = redisTemplate.opsForValue();
        String redisCode = ops.get(toEmail);
        return code.equals(redisCode);
    }
}
